<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class xuq_uqcms extends control
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    public function get_one($id)
    {
        return $this->module('xuq')->get_one($id);
    }
    
    public function ajax_get_next_list()
    {
    	$pcid = isset($_POST['pcid']) ? $_POST['pcid'] : '0';
    	$query = $this->db->get_all("select * from " . table('fbxq') . " where pcid = " . $pcid . " and status = 1");
    	if ($query) {
    		foreach ($query as $k => $v) {
    			$clist[] = $v;
    		}
    		if (isset($clist)) {
    			$data['error'] = '0';
    			$data['last'] = '0';
    			$data['category'] = $clist;
    			echo json_encode($data);
    		} else {
    			$data['error'] = '0';
    			$data['last'] = '1';
    			echo json_encode($data);
    		}
    	} else {
    		$data['error'] = '0';
    		$data['last'] = '1';
    		echo json_encode($data);
    	}
    }
    
    public function ajax_get_gg()
    {
    	 $pcid = isset($_POST['pcid']) ? $_POST['pcid'] : '0';
    	 
    	 $sql="";
    	 
    	  $row = $this->db->get_one("select pcid from swt_category where cid=$pcid and status = 1");
    	 $pcid=$row['pcid'];

    	 $query = $this->db->get_all("select id,name from " . table('goods_sku') . " where cid = " . $pcid ." or (cid=0 and pid=0)" );
    	 if ($query) {
    	 	$i=0;
    	 	foreach ($query as $k => $v) {
    	 		$clist[$i]['yiji'] = $v;
    	 		$clist[$i]['yiji']['erji'] = $this->db->get_all("select id,name from " . table('goods_sku') . " where pid = " . $v['id'] );
    	 		$i++;
    	 	}
    	 	
    	 
    	 	if (isset($clist)) {
    	 		$data['error'] = '0';
    	 		$data['last'] = '0';
    	 		$data['category'] = $clist;
    	 		echo json_encode($data);
    	 	} else {
    	 		$data['error'] = '0';
    	 		$data['last'] = '1';

    	 	}
    	 }		
    	
    }
    
    public function edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->module('address')->get_one($id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    
    function edit_save()
    {
    	
    
        if ($_POST) {
        	
            $setarr['title'] = $_REQUEST['name'];
            $setarr['goods_fl'] = $_REQUEST['mobile'];
            $sl =$_REQUEST['sl'];
            $gg =$_REQUEST['gg'];
            $setarr['bz'] = !empty($_POST['bz']) ? trim($_POST['bz']) : '';
            $setarr['addtime'] = time();
         	$sl=explode(',', $sl);
         	
         	$gg=explode(';', $gg);
         	
         
         	$i=0;
         		foreach ($gg as $gg1){		
         			$gg2=explode(',', $gg1);
         			$goods[$i]=$gg2;
         			array_push($goods[$i],"数量",$sl[$i]);
         			$i++;
         			
         		}

         		$setarr['goods_guige']=serialize($goods);
         		$setarr['user_id']=$_SESSION['user']['uid'];
         		$query = $this->db->add(table('fbxq'), $setarr);
         		
               // $query = $this->db->update(table('member_address'), $setarr, 'uid = ' . $this->uid . ' and id = ' . $id);
      
            if (!empty($_POST['is_default'])) {
                $this->set_default_func($id);
            }
            if ($query) {
                $result = $setarr;
                $result['id'] = $id;
                $result['error'] = '0';
                $result['msg'] = '保存成功';
                echo json_encode($result);
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('提交错误');
        $query = $this->db->delete(table('member_address'), 'uid = ' . $this->uid . ' and id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
    function set_default()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('访问错误');
        $query = $this->set_default_func($id);
        if ($query) {
            right_json();
        } else {
            error_json('设置失败');
        }
    }
    function set_default_func($id)
    {
        $this->db->autocommit();
        $query1 = $this->db->update(table('member_address'), array('is_default' => '0'), 'uid = ' . $this->uid);
        $query2 = $this->db->update(table('member_address'), array('is_default' => '1'), 'uid = ' . $this->uid . ' and id = ' . $id);
        if ($query1 && $query2) {
            $this->db->commit();
            return true;
        } else {
            $this->db->rollback();
            return false;
        }
    }
    
    function seller_detail()
    {
    	$id = $_GET['id'];
    	if(!empty($id)){
    		$sql = "select f.*,u.nick,c.name from " . table('fbxq') . " as f left join ".table('member')." as u on f.user_id = u.uid left join ".table('category')." as c on f.goods_fl = c.cid where id = '{$id}' limit 1";
    		$detail = $this->db->get_one($sql);
    		$goods = unserialize($detail['goods_guige']);
    		$guige = '';
    		foreach ($goods as $kk => $g_value){
    			foreach ($g_value as $g_k => $g_v){
    				if($g_k == 0 && $kk != 0){
    					$guige .= '<a class="guige">'.$g_v.'：</a>';
    				}else{
    					$guige .= $g_v;
    				}
    			}
    		}
    		$detail['goods_guige'] = $guige;
    		$tb_sql = "select t.*,u.nick from ".table('tb')." as t left join ".table('member')." as u on t.mid = u.uid where tb_id = ".$id;
    		$tb = $this->db->get_all($tb_sql);
    		$this->assign('tb',$tb);
    		$this->assign('detail',$detail);
    	}else{
    		header("Location:admin.php?m=goods_order&a=seller");
    	}
    	$this->display();
    }
    

}